$(function () {
    //常用jQuery对象
    var emp_datagrid = $("#emp_list");
    var emp_dailog = $("#emp_dialog");
    var emo_form = $("#emp_form");
    var resetPassword_dialog = $("#resetPassword_dialog");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {

            emp_dailog.dialog("open");
            emp_dailog.dialog("setTitle", "新增");
            $("#password").show();
        },

        //编辑按钮方法
        edit: function () {
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $("#password").hide();
            emp_dailog.dialog("open");
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            emp_dailog.dialog("setTitle", "编辑");
            emp_dailog.form("load", row);
            $.get("/role/getRoleByEmployeeId.do", {employeeId: row.id}, function (data) {
                $("#roleId").combobox("setValues", data);
            }, "json");


        },
        //导出文件模板
        getImportXlsMode: function () {
            window.open("/employee/getImportXlsMode.do");
        },
        //上传excel表格
        importXls: function () {
            $("#importXls_form").form("submit", {
                url: "/employee/importXls.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            emp_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                    }
                }
            })
        },

        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            emo_form.form("submit", {
                url: "/employee/saveOrUpdate.do",
                onSubmit: function (param) {
                    var roles = $("#roleId").combobox("getValues");
                    for (var i = 0; i < roles.length; i++) {
                        param["roles[" + i + "].id"] = roles[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            emp_dailog.dialog("close");
                            emp_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            emp_dailog.dialog("close");
                        });
                    }
                }
            })
        },


        //设置重置密码
        resetPassword: function () {
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            resetPassword_dialog.dialog("open");
            resetPassword_dialog.form("load", row);
        },

        //重置密码具体逻辑
        resetPWLogic: function () {
            var resetPassword = $("#resetPassword").textbox("getValue");
            var repassword = $("#repassword").textbox("getValue");

            if (resetPassword != repassword) {
                $.messager.alert("温馨提示", "两次密码不一致", "info");
                return;
            }
            $("#password_form").form("submit", {
                url: "/employee/resetpassword.do",
                success: function (data) {
                    // data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            resetPassword_dialog.dialog("close");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            resetPassword_dialog.dialog("close");
                        });
                    }
                }
            });

        },


        //设置离职按钮
        changeState: function () {
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            var entity_state = "亲,您确定要为其复职吗？";
            if (row.state) {
                var entity_state = "亲,您确定要设置成离职吗？";
            }
            $.messager.confirm('温馨提示', entity_state, function (r) {
                if (r) {
                    $.get("/employee/changState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                emp_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            $("#keyword").textbox("setValue", "");
            $("#deptId").combobox("setValue", "");
            var beginDate = $("#beginDate").datebox("setValue", "");
            var endDate = $("#endDate").datebox("setValue", "");
            emp_datagrid.datagrid("reload", {deptId: "-1"});
        },


        //高级查询按钮
        searchs: function () {
            var keyword = $("#keyword").textbox("getValue");
            var deptId = $("#deptId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            emp_datagrid.datagrid("load", {
                keyword: keyword,
                deptId: deptId,
                beginDate: beginDate,
                endDate: endDate
            });
        }
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    emp_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/employee/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_changeState").linkbutton({
                    text: "设置离职"
                });
            } else {
                $("#btn_changeState").linkbutton({
                    text: "设置复职"
                });

            }
        },
        columns: [[
            {field: 'username', title: '用户名', width: 100},
            {field: 'realname', title: '真实名称', width: 100},
            {field: 'tel', title: '电话号码', width: 100},
            {field: 'email', title: '电子邮箱', width: 100},
            {
                field: 'dept', title: '所属部门', width: 100, formatter: function (dept) {
                    return dept ? dept.name : dept;
                }
            },
            {field: 'hireDate', title: '雇佣日期', width: 100},
            {
                field: 'state', title: '在职状态', width: 100, formatter: function (state) {
                    return state ? "在职" : "<font color='red'>离职</font>";
                }
            },
            {
                field: 'admin', title: '管理员', width: 100, formatter: function (admin) {
                    return admin ? "是" : "否";
                }
            }
        ]],
        toolbar: "#tools"
    });

    //编辑添加弹出框设置
    emp_dailog.dialog({
        width: 350,
        height: 350,
        closed: true,
        onClose: function () {
            emo_form.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                emp_dailog.dialog("close");
            }
        }]
    });

    //重置密码弹出框
    resetPassword_dialog.dialog({
        title: "重置密码",
        width: 350,
        height: 200,
        closed: true,
        onBeforeOpen: function () {
            $("#repassword").textbox("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["resetPWLogic"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                resetPassword_dialog.dialog("close");
            }
        }]
    });

    //form表单设置
    emp_dailog.form({
        fit: true
    })

    //新增默认部门设置
    $("#depts").combobox("select", "1");

});

